################################################################################
# IMPORTS
################################################################################

import datetime

from django.conf import settings
from django.contrib.sitemaps import Sitemap

from taxonomy.models import Term, get_roots

################################################################################
# UTILITY FUNCTIONS
################################################################################
def convert_changefreq(freq):
    """
    return a number based on change freq string

    @freq: the frequency string
    """
    if freq == 'always':
        return 0
    elif freq == 'hourly':
        return 1
    elif freq == 'daily':
        return 2
    elif freq == 'weekly':
        return 3
    elif freq == 'monthly':
        return 4
    elif freq == 'yearly':
        return 5
    elif freq == 'never':
        return 6
    else:
        return 7

################################################################################
# CLASS TaxonomySitemap
################################################################################
class TaxonomySitemap(Sitemap):
    """
    This class is provided to help you integrate taxonomy related instance and
    taxonomy term that specifically sets the 'include_in_sitemap' field to True
    in the global sitemap

    Just include in your sitemap dictionnary.

    See django.contrib.sitemaps for more information on the sitemaps framework.
    """

    def items(self, term=None):
        """
        returns the list of items to include in the sitemap

        @term: leave to None, used for recursion in term children.
        """
        objs = []
        if term:
            children = list(term.get_children())
        else:
            chilren = list(get_roots())

        for child in children:
            rdata = child.get_render_data(1)
            ilist = []
            for instance in rdata['instances']:
                if not 'url' in instance:
                    continue
                ilist.append({
                    'lastmod':instance['pubdate'] if 'pubdate' in instance else datetime.datetime.now(),
                    'changefreq':instance['changefreq'] if 'changefreq' in instance else 'never',
                    'priority':instance['priority'] if 'priority' in instance else 0.5,
                    'location':instance['url']
                })
            if child.include_in_sitemap:
                lastmod = datetime.datetime(1970,1,1,0,0,0) # the epoch
                changefreq = 'never'
                priority = 0
                for i in ilist:
                    if i['lastmod'] > lastmod:
                        lastmod = i['lastmod']
                    if convert_changefreq(i['changefreq']) < convert_changefreq(changefreq):
                        changefreq = i['changefreq']
                    if i['priority'] > priority:
                        priority = i['priority']
                objs.append({
                    'lastmod':lastmod,
                    'changefreq':changefreq,
                    'priority':priority,
                    'location':child.get_absolute_url()
                })
            objs.append(ilist)
            if len(rdata['children']) > 0:
                objs.append(self.items(child))

    def lastmod(self, item):
        """
        returns the last modification date of an item

        @item: The item from which to get the last modification date from
               as returned by self.items
        """
        return item['lastmod']

    def changefreq(self, item):
        """
        returns the change frequency of an item

        @item: The item from which to get the change frequency from
               as returned by self.items
        """
        return item['changefreq']

    def priority(self, item):
        """
        returns the priority of an item

        @item: The item from which to get the priority from
               as returned by self.items
        """
        return item['priority']

    def location(self, item):
        """
        returns the location of an item

        @item: The item from which to get the location from
               as returned by self.items
        """
        return item['location']

